package com.zzh.takeoutapp.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zzh.takeoutapp.R;


public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    protected View rootView;
    //当前Fragment是否处于可见状态标志，防止因ViewPager的缓存机制而导致回调函数的触发
    private boolean isFragmentVisible;
    //是否是第一次开启网络加载
    public boolean isFirst;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isFragmentVisible = true;
        }
        if (rootView == null) {
            return;
        }
        //可见，并且没有加载过
        if (!isFirst && isFragmentVisible) {
            onFragmentVisibleChange(true);
            return;
        }
        //由可见——>不可见 已经加载过
        if (isFragmentVisible) {
            onFragmentVisibleChange(false);
            isFragmentVisible = false;
        }
    }

    protected void onFragmentVisibleChange(boolean isVisible) {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getsavedInstanceState(savedInstanceState);
        rootView = onCreateView(inflater, container);
        initComponent(rootView);
        setListener();
        onAfterInit();
        return rootView;
    }

    /**
     * 获取保存数据
     *
     * @param savedInstanceState
     */
    public abstract void getsavedInstanceState(Bundle savedInstanceState);

    /**
     * 设置显示页面
     */
    public abstract View onCreateView(LayoutInflater inflater, ViewGroup container);

    /**
     * 初始化界面控件
     *
     * @param view 控件所在View
     */
    public abstract void initComponent(View view);

    /**
     * 各种事件
     */
    public abstract void setListener();

    /**
     * 最后要做的
     */
    public abstract void onAfterInit();

    /**
     * 打开Activity, 伴随动画
     *
     * @param intent
     */
    public void start_Activity(Intent intent) {
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    /**
     * 打开Activity, 伴随动画
     *
     * @param intent
     */
    public void start_ActivityForResult(Intent intent, int requestCode) {
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, requestCode);
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    public void finish_Activity() {
        getActivity().finish();
        getActivity().overridePendingTransition(0, R.anim.push_right_out);
    }
}
